//
//  Alamofire+WMUIExtension.swift
//  ChengLuMall
//
//  Created by 吳梓杭 on 2023/10/12.
//  

import Foundation
import Alamofire
import SwiftyJSON

//extension Array where Element: HandyJSON {
//    
//    public var toObject: [String : Any] {
//        return ["jsonArray" : self.toRequestJSON()]
//    }
//    
//}
 
struct JSONArrayEncoding: ParameterEncoding {
    static let `default` = JSONArrayEncoding()

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var request = try urlRequest.asURLRequest()

        guard let json = parameters?["jsonArray"] else {
            return request
        }
        
        let data = try? JSON(json).rawData()
        
        if request.value(forHTTPHeaderField: "Content-Type") == nil {
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        }

        request.httpBody = data

        return request
    }
}
